<?php
namespace App\Http\Controllers\Admin;
use App\http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

class GoodController extends BaseController{
    //商品列表页面
    public function good_list(){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $cat = DB::table("classify")->get();
        return view('admin/good/good_list',["cat"=>$cat]);
    }
    //获取博客列表数据
    public function toget_good_list(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $good_p = DB::table("good as g")
        ->leftJoin("classify as c","c.classify_id","g.classify_id")
        ->orderBy("g.good_addtime","DESC");
        if($request->good_name){
            $good_p = $good_p->where("good_name","like","%".$request->good_name."%");
        }
        if($request->classify_id){
            $good_p = $good_p->where("g.classify_id",$request->classify_id);
        }
        if($request->start){
            $good_p = $good_p->where("good_addtime",">=",strtotime($request->start));
        }
        if($request->end){
            $good_p = $good_p->where("good_addtime","<=",strtotime($request->end));
        }
        $good_p = $good_p->paginate(5);
        // ->paginate(5);
        // dd($good);
        // echo $good_p->render();
        // dd($request->op);
        // die();
		$good = $good_p->items();
        $current = $good_p->currentPage();
        $a = ($current-1)*5+1;
        echo '
        <div class="layui-form" >
        <table class="layui-table">
            <thead>
                <tr>
                    <th>
                    <input type="checkbox" name="all" value="" lay-skin="primary" lay-filter="all" />
                    </th>
                    <th>编号</th>
                    <th>商品名称</th>
                    <th>上架时间</th>
                    <th>分类</th>
                    <th>价格</th>
                    <th>推荐</th>
                    <th>新品</th>
                    <th >操作</th>
                </tr>
            </thead>
            <tbody>
        ';
        // print_r($good);
        foreach ($good as  $value) {
            $good_addtime = date("Y-m-d",$value->good_addtime);
            $good_id = $value->good_id;
            echo '
            <tr>
                <td>
                <input type="checkbox" name="id[]" value="'.$good_id.'" lay-skin="primary" lay-filter="item" />
                </td>
                <td>'.$a++.'</td>
                <td>'.$value->good_name.'</td>
                <td>'.$good_addtime.'</td>
                <td>'.$value->classify_name.'</td>
                <td>'.$value->good_price.'</td>';
                if ($value->recommand==1) {
                    echo '<td><input type="checkbox" name="recommand" lay-filter="recommand" gid='.$value->good_id.' lay-skin="switch" lay-text="ON|OFF" checked></td>';
                }else{
                    echo '<td><input type="checkbox" name="recommand" lay-filter="recommand" gid='.$value->good_id.' lay-skin="switch" lay-text="ON|OFF"></td>';
                }
                if ($value->isnew==1) {
                    echo '<td><input type="checkbox" name="isnew" lay-filter="isnew" gid='.$value->good_id.' lay-skin="switch" lay-text="ON|OFF" checked></td>';
                }else{
                    echo '<td><input type="checkbox" name="isnew" lay-filter="isnew" gid='.$value->good_id.' lay-skin="switch" lay-text="ON|OFF"></td>';
                }
                echo '
                <td class="td-manage">
                <a title="查看"  onclick="x_admin_show('."'编辑'".','."'/admin/edit_good?gid=$good_id'".')" href="javascript:;">
                    <i class="layui-icon">&#xe63c;</i>
                </a>
                <a title="删除" onclick="member_del(this,'.$good_id.')" href="javascript:;">
                    <i class="layui-icon">&#xe640;</i>
                </a>
                <a title="添加商品图片"  onclick="x_admin_show('."'添加商品图片'".','."'/admin/add_imgs?good_id=$good_id'".')" href="javascript:;">
                    <i class="layui-icon">&#xe61f;</i>
                </a>
                </td>
            </tr>
            ';
        }
        echo '
        </tbody>
        </table>
        </div>
        <div class="page">'.$good_p->render().'</div>
        ';
    }
    //获取商品总数
    public function toget_good_count(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $good = DB::table('good');
        if($request->good_name){
            $good = $good->where("good_name","like","%".$request->good_name."%");
        }
        if($request->classify_id){
            $good = $good->where("classify_id",$request->classify_id);
        }
        if($request->start){
            $good = $good->where("good_addtime",">=",strtotime($request->start));
        }
        if($request->end){
            $good = $good->where("good_addtime","<=",strtotime($request->end));
        }
        $good = $good->count();
        // ->count();
        echo $good;
    }
    //改变商品是否推荐状态，最多五个
    public function tochange_recommand(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        if ($request->gid!=null) {
            $gid =$request->gid;
            $changeto = $request->changeto;
            if($changeto==1){//如果从off改到on要判断推荐的条数是否溢出
               $is_overflow = DB::table("good")->where("recommand","1")->count();
               if($is_overflow>=5){//已满或溢出
                   echo json_encode(array(
                        "code"=>2,
                        "msg"=>"推荐商品最多五个"
                   ));die;
               }else{//未满
                    DB::table("good")->where("good_id",$gid)->update(["recommand"=>1]);
                    echo json_encode(array(
                        "code"=>1,
                        "msg"=>"修改成功"
                   ));die;
               }
            }else{
                DB::table("good")->where("good_id",$gid)->update(["recommand"=>2]);
                echo json_encode(array(
                    "code"=>1,
                    "msg"=>"修改成功"
               ));die;
            }
            
        }
    }
    //改变商品是否新品状态
    public function tochange_isnew(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        if ($request->gid!=null) {
            $gid =$request->gid;
            $changeto = $request->changeto;
            if($changeto==1){//如果从off改到on
                DB::table("good")->where("good_id",$gid)->update(["isnew"=>1]);
                echo json_encode(array(
                    "code"=>1,
                    "msg"=>"修改成功"
                ));die;
            }else{
                DB::table("good")->where("good_id",$gid)->update(["isnew"=>2]);
                echo json_encode(array(
                    "code"=>1,
                    "msg"=>"修改成功"
               ));die;
            }
            
        }
    }
    //商品添加页面
    public function add_good(){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $cat = DB::table("classify")->get();
        // dd($cat);die;
        return view("admin/good/good_add",["cat"=>$cat]);
    }
    //商品添加异步
    public function todo_addgood(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $data = array(
            "good_name"=>$request->good_name,
            "good_price"=>$request->good_price,
            "good_dec"=>$request->good_dec,
            "good_info"=>$request->good_info,
            "recommand"=>$request->recommand,
            "isnew"=>$request->isnew,
            "classify_id"=>$request->classify_id,
            "good_addtime"=>strtotime($request->good_addtime)
        );
        if($request->hasFile('main_img')&&$request->file('main_img')->isValid()){
            $file=$request->file('main_img');
            $allowed_extensions = ["png", "jpg", "gif"];
            if (!in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                echo json_encode(array(
                    "code"=>"2",
                    "msg"=>"只能上传png,jpg和gif格式的图片"
                ));die;
            }else{
                $destinationPath = 'uploads/main_img/'; //public 文件夹下面建 uploads 文件夹
                $extension = $file->getClientOriginalExtension();
                $fileName=md5(time().rand(1,1000)).'.'.$extension;
                $file->move($destinationPath,$fileName);
                $data["main_img"]="main_img/".$fileName;
            }
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"图片上传失败请重试"
            ));die;
        }
        $res = DB::table("good")->insert($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"添加成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
    //商品编辑页面
    public function edit_good(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $good_id = $request->gid;
        $good = DB::table("good")->where("good_id",$good_id)->first();
        $good->good_addtime = date("Y-m-d",$good->good_addtime);
        $cat = DB::table("classify")->get();
        // dd($cat);die;
        return view("admin/good/good_edit",[
                        "cat"=>$cat,
                        "good"=>$good
                    ]);
    }
    //商品编辑异步
    public function todo_editgood(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $data = array(
            "good_name"=>$request->good_name,
            "good_price"=>$request->good_price,
            "good_dec"=>$request->good_dec,
            "good_info"=>$request->good_info,
            "recommand"=>$request->recommand,
            "isnew"=>$request->isnew,
            "classify_id"=>$request->classify_id,
            "good_addtime"=>strtotime($request->good_addtime)
        );
        if($request->hasFile('file')&&$request->file('file')->isValid()){
            $file=$request->file('file');
            $allowed_extensions = ["png", "jpg", "gif"];
            if (!in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                echo json_encode(array(
                    "code"=>"2",
                    "msg"=>"只能上传png,jpg和gif格式的图片"
                ));die;
            }else{
                //删除旧图片
                // echo "图片准备删除";die;
                if ($request->old_img&&file_exists("./uploads/".$request->old_img)) {
                    $public = Storage::disk("local");
                    $public->delete("uploads/".$request->old_img);
                }
                $destinationPath = 'uploads/main_img/'; //public 文件夹下面建 uploads 文件夹
                $extension = $file->getClientOriginalExtension();
                $fileName=md5(time().rand(1,1000)).'.'.$extension;
                $file->move($destinationPath,$fileName);
                $data["main_img"]="main_img/".$fileName;
            }
        }
        $res = DB::table("good")->where("good_id",$request->good_id)->update($data);
        if($res){
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"保存成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
    //商品删除
    public function todo_delgood(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        $good_id = $request->good_id;
        $good = DB::table("good")->where("good_id",$good_id)->first();
        $img_path = $good->main_img;
        if ($img_path&&file_exists("./uploads/".$img_path)) {
            $public = Storage::disk("local");
            $public->delete("uploads/".$img_path);
        }
        $res = DB::table("good")->where("good_id",$good_id)->delete();
        if ($res) {
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"删除成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据库执行有误"
            ));die;
        }
    }
    //批量删除
    public function todo_delgoods(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        // print_r($request->all());
        $idarr = $request->id;
        foreach($idarr as $val){
            $good_id = $val;
            $good = DB::table("good")->where("good_id",$good_id)->first();
            $img_path = $good->main_img;
            if ($img_path&&file_exists("./uploads/".$img_path)) {
                $public = Storage::disk("local");
                $public->delete("uploads/".$img_path);
            }
        }
        $res = DB::table("good")->whereIn("good_id",$idarr)->delete();
        if ($res) {
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"删除成功"
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据库执行有误"
            ));die;
        }
    }
    public function test(Request $request){
        $islogin = $this->islogin();
        if (!$islogin) {
            return redirect("/admin/login");
        }
        if ($request->old_img&&file_exists("./uploads/".$request->old_img)) {
           
            if (file_exists("./uploads/".$request->old_img)) {
                echo "删除失败";
            }else{
                echo "删除成功";
            }
        }else{
            echo "文件不存在";
        }
    }

    //商品图片上传
    public function add_imgs(Request $request){
        $good_id = $request->good_id;
        $images = DB::table("goodimg")->where("good_id",$good_id)->get();
        return view('admin/good/add_imgs',["good_id"=>$good_id,"images"=>$images]);
    }

    //商品图片上传执行
    public function todo_addimgs(Request $request){
        $good_id = $request->good_id;
        $data = array(
            "good_id"=>$good_id
        );
        if($request->hasFile('main_img')&&$request->file('main_img')->isValid()){
            $file=$request->file('main_img');
            $allowed_extensions = ["png", "jpg", "gif"];
            if (!in_array($file->getClientOriginalExtension(), $allowed_extensions)) {
                echo json_encode(array(
                    "code"=>"2",
                    "msg"=>"只能上传png,jpg和gif格式的图片"
                ));die;
            }else{
                $destinationPath = 'uploads/good_imgs/'; //public 文件夹下面建 uploads 文件夹
                $extension = $file->getClientOriginalExtension();
                $fileName=md5(time().rand(1,1000)).'.'.$extension;
                $file->move($destinationPath,$fileName);
                $data["images"]="good_imgs/".$fileName;
                $res = DB::table("goodimg")->insert($data);
                if ($res) {
                    echo json_encode(array(
                        "code"=>"1",
                        "msg"=>"图片上传成功",
                        "name"=>$data["images"]
                    ));die;
                }else{
                    echo json_encode(array(
                        "code"=>"2",
                        "msg"=>"数据执行有误"
                    ));die;
                }
            }
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"图片上传有误"
            ));die;
        }
    }

    //商品图片删除
    public function todo_delgoodimg(Request $request){
        $path = $request->path;
        $images = substr($path,9);
        $res = DB::table("goodimg")->where("images",$images)->delete();
        if ($res) {
            unlink(".".$path);
            echo json_encode(array(
                "code"=>"1",
                "msg"=>"删除成功",
            ));die;
        }else{
            echo json_encode(array(
                "code"=>"2",
                "msg"=>"数据执行有误"
            ));die;
        }
    }
}

